package com.action.gulimall.order.interceptor;

import com.action.common.constant.AuthServerConstant;
import com.action.common.constant.CartConstant;
import com.action.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author sl shilun217@qq.com
 * @Deacription 登录拦截器，没有登录的请求需要先去登录
 * @date 2021/07/03/15:12
 **/
@Component
public class LoginInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResponseVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        boolean match = new AntPathMatcher().match("/order/order/listWithItem", request.getRequestURI());
        if(match){
            return true;
        }
        String out_trade_no = request.getParameter("out_trade_no");
        if(!StringUtils.isEmpty(out_trade_no)){
            return true;
        }
        MemberResponseVo memberResponseVo = (MemberResponseVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if(memberResponseVo!=null){
            threadLocal.set(memberResponseVo);
            return true;
        }else{
            //去登录
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }

    }
}
